<?php
class Admin extends CActiveRecord{

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'admin';
    }
    
    public function validatePassword($password)
    {
        return $this->hashPassword($password)===$this->password;
    }
 
    public function hashPassword($password)
    {
        return md5($password);
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function getRows(){        
        $row = array();
        $sql = "SELECT * FROM admin";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row;
    }   

    public function getRowsByUsername($title){

        $row = array();        
        $sql = "SELECT * FROM admin WHERE `username`='".$title."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryRow();                        
        return $row;
    }

    public function getRowsById($id){

        $row = array();        
        $sql = "SELECT * FROM admin WHERE `id` = '".$id."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryRow();                        
        return $row;
    }

    public function getRowsByUserPaging($begin,$end){        
        $row = array();        
        $sql = "SELECT * FROM admin LIMIT ".$begin.",".$end;            
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();            
        return $row;
    }

    public function getRowsByTitleUserPaging($title,$begin,$end){        
        $row = array();        
        $sql = "SELECT * FROM admin WHERE `username` LIKE '%".$title."%'  LIMIT ".$begin.",".$end;            
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryAll();                        
        return $row;
    }

    public function deleteRowById($id)
    {
        $row = array();                        
        $sql = "DELETE FROM admin WHERE `id` = ".$id." ";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }
    
    public function getPermit($user_id){
        $sql = "SELECT itemname,module FROM authassignment WHERE userid=".$user_id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();
        return $rows;
    }

    public function updateRowById($id,$password)
    {
        $row = array();                        
        $sql = "UPDATE admin SET `password`='".$password."' WHERE `id` = ".$id." ";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }
    
    public function insertRow($username,$password,$fullname,$user_create){   
        $pass = AdminLogin::hashPassword($password);     
        $date = date('Y-m-d',time());
        $row = array();              
        $sql = "INSERT INTO admin (username,password,fullname,create_date,create_user) VALUES ('".$username."','".$pass."','".$fullname."','".$date."','".$user_create."')";
        //echo $sql;die;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute();            
        return $result;
    }
    
    public function insertPermit($user_id,$arr_item){   
        $str = "";    
        if(count($arr_item)>0){  
            foreach($arr_item as $key=>$value){
                $str .= "(".$user_id.",'".$value."'),";
            }
            $str = rtrim($str,",");
            $sql = "INSERT INTO authassignment(userid,itemname) VALUES ".$str;            
            $connect = Yii::app()->db;
            $command =$connect->createCommand($sql);
            $result = $command->execute();
            return $result; 
        }else{
            return 1;
        }      
    }
    public function deletePermit($user_id,$arr_item){
        $sql = "";
        if(count($arr_item)>0){
            foreach($arr_item as $key=>$value){
                $sql .= "DELETE FROM authassignment WHERE userid =".$user_id." AND itemname = '".$value."';"; 
            }   
            $connect = Yii::app()->db;
            $command =$connect->createCommand($sql);
            $result = $command->execute();
            return $result;
        }else{
            return 1;
        }
    }
}
